<template>
  <div class="building">
    <el-card>
      <el-form inline>
        <el-form-item label="楼宇名称">
          <el-input size="small" placeholder="请输入楼宇名称" v-model="pageParams.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" v-model="pageParams.name" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button size="small" icon="el-icon-plus" type="success" @click="handleAdd">添加楼宇</el-button>
      <el-table border stripe size="small" :header-cell-style="{background:'#4d8ed9',color:'#fff'}" :data="buildingList" v-loading="loading" element-loading-text="拼命加载中">
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="楼宇名称" prop="name" align="center"></el-table-column>
        <el-table-column label="层数" prop="floors" align="center"></el-table-column>
        <el-table-column label="在管面积(m²)" prop="area" align="center"></el-table-column>
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" align="center"></el-table-column>
        <el-table-column label="状态" :formatter="formatterStatus" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="pageParams.page" :page-sizes="[15, 20, 30, 40]"
        :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total" background
      >
      </el-pagination>
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="35%" @close="handleCloseDialog">
      <building-form :edit="edit" @success="handleSuccess" ref="buildingForm" @cancel="dialogVisible=false"></building-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import BuildingForm from '@/views/park/building/components/buildingForm/buildingForm.vue'

export default {
  name: 'building',
  components: { BuildingForm },
  data() {
    return {
      /* 分页参数 */
      pageParams: {
        name: undefined,
        page: 1,
        pageSize: 15
      },
      /* 加载状态 */
      loading: false,
      /* 是否显示对话框组件 */
      dialogVisible: false,
      /* 是否处于编辑状态 */
      edit: false
    }
  },
  methods: {
    /* 获取楼宇列表的回调函数 */
    async fetchBuildingList() {
      try {
        this.loading = true
        await this.$store.dispatch('building/fetchBuildingList', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 格式化状态的回调函数 */
    formatterStatus(row, column, cellValue, index) {
      const MAP = { 0: '空置中', 1: '租赁中' }
      return MAP[row.status] || '未知'
    },
    /* 点击删除按钮的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`删除${row.name}, 是否继续?`, '提示', { type: 'warning' }).catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('building/deleteBuilding', row.id)
        this.$message.success(deleteResult)
        this.fetchBuildingList()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 点击搜索按钮的回调函数 */
    handleSearch() {
      this.pageParams.page = 1
      this.fetchBuildingList()
    },
    /* 分页器size变化的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchBuildingList()
    },
    /* 分页器page变化的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchBuildingList()
    },
    /* 点击添加楼宇的按钮 */
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    /* 点击添加楼宇的按钮 */
    handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      this.$nextTick(()=>{
        this.$refs.buildingForm.findBuilding(row)
      })
    },
    /* 表单组件添加楼宇或者编辑楼宇成功的回调函数 */
    handleSuccess() {
      this.dialogVisible = false
      this.fetchBuildingList()
    },
    /* 对话框组件关闭时候的回调函数 */
    handleCloseDialog() {
      this.$refs.buildingForm.resetForm()
    }
  },
  computed: {
    ...mapGetters('building', ['buildingList', 'total']),
    title() {
      return this.edit ? '编辑楼宇' : '添加楼宇'
    }
  },
  mounted() {
    this.fetchBuildingList()
  }
}
</script>

<style lang="scss" scoped>
.building {
  .el-card {
    margin-bottom: 10px;

    .el-form-item {
      margin-bottom: unset;
    }
  }

  .el-table {
    margin: 10px 0;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }

  ::v-deep .el-dialog__body {
    display: flex;
    justify-content: center;
  }
}
</style>
